package com.injurydetection.www.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.injurydetection.www.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by sunguiyong on 2020/8/10
 */
public class HeartView extends View {
    int scrollIndex = 0;
    List<Double> data = new ArrayList<>();
    private Paint mPaint = new Paint();
    private Path mPath = new Path();
    private float mHeight;
    private float mWidth;

    public HeartView(Context context) {
        super(context);
        for (int i = 0; i < 100; i++) {
            data.add(Math.random() * 10);
        }
    }

    public HeartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HeartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawpa(canvas);
        scrollBy(1, 0);
        postInvalidateDelayed(10);
    }

    private void drawpa(Canvas canvas) {
        mPath.reset();
        //用path模拟一个心电图样式
        mPath.moveTo(0, getHeight() / 2);
        int tmp = 0;
        for (int i = 0; i < 20; i++) {
            mPath.lineTo(tmp + 150, getHeight() / 2);//lineTo 用于进行直线绘制。
            mPath.lineTo(tmp + 200, getHeight() / 3);
            mPath.lineTo(tmp + 250, getHeight() / 2);
            mPath.lineTo(tmp + 300, getHeight() / 4);
            mPath.lineTo(tmp + 350, getHeight() / 3);
            tmp = tmp + 350;
        }
        //设置画笔style
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(2F);
        mPaint.setAntiAlias(true);
        canvas.drawPath(mPath, mPaint);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    private static final String TAG = "HeartView";
}
